package cn.example.zjgsu.service.Impl;

import cn.example.zjgsu.dao.CityMapper;
import cn.example.zjgsu.dao.UserMapper;
import cn.example.zjgsu.pojo.User;
import cn.example.zjgsu.service.LoadPageServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: miaopeihan
 * @date: 2020-03-18  16:44
 */
@Service
public class LoadPageServerImpl implements LoadPageServer {

    @Autowired
    UserMapper userMapper;

    @Autowired
    CityMapper cityMapper;

    @Override
    public boolean isAccountExist(String account,String password) {
        User user=userMapper.selectByPrimaryKey(account);
        if(user!=null&&user.getPassword().equals(password)){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean isRegisterSuccess(String account,String name,String password,String cityName) {
        int cityid=cityMapper.selectByName(cityName).getId();
        User user=new User(account,password,name,cityid);
        int flag=userMapper.insert(user);
        if(flag>0) return true;
        else return false;
    }

    @Override
    public boolean isModifyPasswordSuccess(String account, String oldPassword, String newPassword) {
        User user=userMapper.selectByPrimaryKey(account);
        if(user!=null&&user.getPassword().equals(oldPassword)){
            user.setPassword(newPassword);
            int state=userMapper.updateByPrimaryKey(user);
            if(state>0) return true;
            else return false;
        }
        return false;
    }
}
